<?php

namespace App\Http\Controllers\Resume;

use App\Http\Controllers\BaseApiController;
use App\Http\Controllers\TranForm\ResumeTransForm;

use App\Model\Industry;
use App\Services\Resume\ResumeServices;
use EasyWeChat\Factory;
use function GuzzleHttp\Promise\all;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Validator;

class ResumeController extends BaseApiController{

    private $service;

    public function __construct(ResumeServices $service){
        //继承基类的构造方法
        parent::__construct();
        //依赖注入service
        $this->service=$service;
    }

    // 简历列表
    public function index(){
        // 调用service层查简历列表
        $resume_list = $this->service->resumeList();
        if(isset($resume_list)){
            // 查询成功进行字段映射
            return $this->response()->collection($resume_list,new ResumeTransForm());
        }else{
            // 未找到
            return $this->response()->errorNotFound();
        }
    }

    // 简历详情
    public function resumeDetails($id){
        // 简历详情
        $resume_details = $this->service->resumeDetails($id);
        if(isset($resume_details)){
             // 单一字段映射
             return $this->item($resume_details,new ResumeTransForm())->content();
        }else{
            // 未找到
            return $this->response()->errorNotFound();
        }
    }

    // 行业下拉框
    public function industry(){
        $array = Industry::all()->toArray();
        array_unshift($array, ['industry_id'=>0,'industry_name'=>'请选择']);//向数组插入元素
        return Response::json($array);
    }

    // 点击创建简历按钮后生成用户基本信息简历
    public function create(Request $request){
        $data = $request->all();
        $data['resume_enabled'] = '0';
        $id = $this->service->createBaseResume($data);
        return json_encode($id);
    }

    /**
     * url == 》
     * 编辑简历 - 求职意向信息
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse|void
     */
    public function editResumeObjective(Request $request){
        if($request->isMethod('post')){
            $data = $request->all();
            $position = explode("~",$data['salary']);
            $data['resume_min'] = $position[0]; // 最低薪资
            $data['resume_max'] = $position[1]; // 最高薪资
            unset($data['salary']);
            // 验证求职意向信息
            $validate = Validator::make($data, [
                'resume_expected_position' => 'max:255', // 期望职位
                'resume_min' => 'numeric', // 期望月薪
                'resume_max' => 'numeric',
                'resume_work' => 'max:255', // 所属行业
                'resume_working_site' => 'max:255', // 工作地点
                'resume_work_status' => 'max:255', // 工作状态
            ]);
            // 未通过验证给出提示
            if($validate->fails()){
                $error = $validate->errors()->all();
                return $this->response()->error($error[0],'404');
            }
            // 判断是添加还是修改
            if($data['operate'] == "modify"){
                unset($data['operate']);
                $data['resume_enabled'] = '1';
                // 修改
                if($this->service->editResumeObjective($data)){
                    return $this->success('求职意向修改成功');
                }else{
                    return $this->response()->error('Modify the failure','403');
                }
            }else if($data['operate'] == "create"){
                unset($data['operate']);
                $data['resume_enabled'] = '1';
                // 新建简历
                $res = $this->service->createResumeObjective($data);
                if($res['state']){
                    return $this->success(['msg'=>'求职信息添加成功，请完善','resume_id'=>$res['resume_id']]);
                }else{
                    return $this->response()->error('Modify the failure','403');
                }
            }
        }
    }

    // 编辑简历 - 基本信息
    public function editResumeBasicInfo(Request $request){
        if($request->isMethod('post')){
            $data = $request->all();
            // 验证简历基本信息
            $validate = Validator::make($data, [
                'resume_real_name' => 'max:255', // 真实姓名
                'resume_user_sex' => 'max:30', // 公司规模
                'resume_born' => 'max:255', // 出生年月
                'resume_lives_city' => 'max:255', // 现居城市
                'resume_native_place' => 'max:255', // 籍贯
                'resume_political_face' => 'max:100', // 政治面容
                'resume_lives_city' => 'max:255', // 手机号
                'resume_native_place' => 'max:255', // 电子邮件
            ]);
            // 未通过验证给出提示
            if($validate->fails()){
                $error = $validate->errors()->all();
                return $this->response()->error($error[0],'404');
            }
            // 判断是添加还是修改
            if($data['operate'] == "modify"){
                unset($data['operate']);
                $data['resume_enabled'] = '1';
                // 修改
                if($this->service->editResumeBasicInfo($data)){
                    return $this->success('简历基本修改成功');
                }else{
                    return $this->response()->error('Modify the failure','403');
                }
            }else if($data['operate'] == "create"){
                unset($data['operate']);
                $data['resume_enabled'] = '1';
                // 新建简历
                $res = $this->service->createResumeBasicInfo($data);
                if($res['state']){
                    return $this->success(['msg'=>'简历基本信息添加成功','resume_id'=>$res['resume_id']]);
                }else{
                    return $this->response()->error('Modify the failure','403');
                }
            }
        }
    }

    // 编辑简历 - 教育背景
    public function editResumeEducation(Request $request){
        if($request->isMethod('post')){
            $data = $request->all();
            // 验证教育背景信息
            $validate = Validator::make($data, [
                'resume_school_name' => 'max:255', // 大学名称
                'resume_education' => 'max:30', // 学历
                'resume_major_name' => 'max:255', // 专业名称
                'resume_admission_date' => 'max:255', // 入学时间
                'resume_graduate_date' => 'max:255', // 毕业时间
            ]);
            // 未通过验证给出提示
            if($validate->fails()){
                $error = $validate->errors()->all();
                return $this->response()->error($error[0],'404');
            }
            // 判断是添加还是修改
            if($data['operate'] == "modify"){
                unset($data['operate']);
                $data['resume_enabled'] = '1';
                // 修改
                if($this->service->editResumeEducation($data)){
                    return $this->success('教育背景修改成功');
                }else{
                    return $this->response()->error('Modify the failure','403');
                }
            }else if($data['operate'] == "create"){
                unset($data['operate']);
                $data['resume_enabled'] = '1';
                // 新建简历
                $res = $this->service->createResumeEducation($data);
                if($res['state']){
                    return $this->success(['msg'=>'教育背景信息添加成功','resume_id'=>$res['resume_id']]);
                }else{
                    return $this->response()->error('Modify the failure','403');
                }
            }
        }
    }

    // 编辑简历 - 实习/工作经历
    public function editResumeInternships(Request $request){
        $data = $request->all();
        // 验证求职意向信息
        $validate = Validator::make($data, [
            'corporate_name' => 'max:255', // 公司名
            'company_scale' => 'max:255', // 公司规模
            'position_anme' => 'max:255', // 职位名称
            'inauguration_time' => 'max:255', // 就职时间
            'work_desc' => 'max:255', // 工作描述
        ]);
        // 未通过验证给出提示
        if($validate->fails()){
            $error = $validate->errors()->all();
            return $this->response()->error($error[0],'404');
        }
        // 判断是添加还是修改
        if($data['operate'] == "modify"){
            unset($data['operate']);
            // 修改
            if($this->service->editResumeInternships($data)){
                return $this->success('实习/工作经历修改成功');
            }else{
                return $this->response()->error('Modify the failure','403');
            }
        }else if($data['operate'] == "create"){
            unset($data['operate']);
            $data['resume_enabled'] = '1';
            // 新建简历
            $res = $this->service->createResumeInternships($data);
            if($res['state']){
                return $this->success(['msg'=>'实习/工作经历添加成功','resume_id'=>$res['resume_id']]);
            }else{
                return $this->response()->error('Modify the failure','403');
            }
        }
    }

    // // 编辑简历 - 比赛奖项
    // public function editResumeCompetitions($id){
    //     echo $id;
    //     echo "比赛奖项";die;
    // }

    // // 编辑简历 - 自我评价
    // public function editResumeSelfReview($id){
    //     echo $id;
    //     echo "自我评价";die;
    // }

    // // 编辑简历 - 实践活动
    // public function editResumePractice($id){
    //     echo $id;
    //     echo "实践活动";die;
    // }

    // // 编辑简历 - 其他信息
    // public function editResumeOther($id){
    //     echo $id;
    //     echo "实践活动";die;
    // }

    // 提交所有剩余信息
    public function editallResumeMsg(Request $request){
        $data = $request->all();
       
        // 验证求职意向信息
        $validate = Validator::make($data, [
            'resume_competitions' => 'max:255', // 比赛奖项
            'resume_comment' => 'max:255', // 自我评价
            'resume_practice' => 'max:255', // 实践活动
            'resume_other' => 'max:255', // 其他信息
        ]);
        // 未通过验证给出提示
        if($validate->fails()){
            $error = $validate->errors()->all();
            return $this->response()->error($error[0],'404');
        }
        // 判断是添加还是修改
        if($data['operate'] == "modify"){
            unset($data['operate']);
            // 修改
            if($this->service->editallResumeMsg($data)){
                return $this->success('实习/工作经历修改成功');
            }else{
                return $this->response()->error('Modify the failure','403');
            }
        }else if($data['operate'] == "create"){
            unset($data['operate']);
            $data['resume_enabled'] = '1';
            // 新建简历
            $res = $this->service->createallResumeMsg($data);
            if($res['state']){
                return $this->success(['msg'=>'实习/工作经历添加成功','resume_id'=>$res['resume_id']]);
            }else{
                return $this->response()->error('Modify the failure','403');
            }
        }
    }
}

